package team.bluepen.supermarket.util;

import java.io.*;

/**
 * 生成用户ID
 * @author Kai
 */
public final class IdGenerator {
    private static final String TAG = "IdGenerator";
    /** 预留ID位数 */
    private static final int ID_START = 4;
    /** 存储当前id */
    private static final String FILE_NAME = "ids.market";

    private IdGenerator() { }

    /**
     * 生成用户ID
     * @return id
     */
    public static long generate() {
        File file = new File(FILE_NAME);
        boolean state = true;
        if (!file.exists()) {
            try {
                if (!file.createNewFile()) {
                    Log.e(TAG, "File create failed.");
                    return -1;
                }
                state = false;
            } catch (IOException e) {
                Log.e(TAG, "An error occurred while generating id.");
            }
        } else {
            if(file.length() <= 0){
                state = false;
            }
        }
        Log.d(TAG, "Loading ids. " + file.getAbsolutePath());
        PrintWriter outputStream = null;
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
            String read = bufferedReader.readLine();
            outputStream = new PrintWriter(file);
            if (!state) {
                // 预留四位
                outputStream.print(ID_START + 1 + "");
                outputStream.flush();
                return ID_START + 1L;
            } else {
                long _id = Long.parseLong(read);
                outputStream.print(_id + 1);
                outputStream.flush();
                return _id + 1;
            }
        } catch (IOException e) {
            Log.e(TAG, "An error occurred while generating id.");
            return 0;
        } finally {
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }

    /**
     * 修改当前ID位置。
     * <h2>程序运行正常情况下请勿使用！</h2>
     * @param id ID
     */
    public static void redirectId(int id) {
        File file = new File(FILE_NAME);
        if (!file.exists()) {
            try {
                if (file.createNewFile()) {
                    Log.e(TAG, "File create failed.");
                    return;
                }
            } catch (IOException e) {
                Log.e(TAG, "An error occurred while redirecting id.");
                return;
            }
        }
        try (PrintWriter outputStream = new PrintWriter(file)) {
            outputStream.print(id);
            outputStream.flush();
        } catch (IOException e) {
            Log.e(TAG, "An error occurred while redirecting id.");
        }
    }

    public static String getNowId() {
        File file = new File(FILE_NAME);
        if (!file.exists()) {
            return "0";
        }
        try (BufferedReader inputStream = new BufferedReader(new FileReader(file))) {
            return inputStream.readLine();
        } catch (IOException e) {
            Log.e(e);
        }
        return "0";
    }
}
